package com.sptpc.demo1.controller;

import com.sptpc.demo1.model.Category;
import com.sptpc.demo1.service.ICategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
 @Autowired
public ICategoryService categoryService;

 @GetMapping("/")
 public List<Category> getAll() {

  return categoryService.getAllCategories();

 }
 //put /category/
 @PutMapping("/")
 public Category modifyCategory(@RequestBody Category request) {
  // log.info("要修改的饮品: {}", request);
  //1.通过id查询饮品
  Category category=categoryService.getCategoryById(request.getId());
  //2.复制前端要修改的属性
  BeanUtils.copyProperties(request,category);
  //3. 更新数据库
  categoryService.updateCategory(category);
  return category;
 }
}
